# The strategy I am using is mainly Tit-For-Tat. At the begining of the game when there should a lot of players the identity of a player can not be known from
#his/her reputation but when number of players fall below the round number it should be possible to identify someone from his/her reputation.
#I hunt with everyone for the first 10 rounds. For the next 40 rounds I only slack with those who never hunted.There maybe some players who will always slack.
#After that, I divide the players according to their reputation in 100 groups. I decide to hunt or slack on the basis of how players in a group behaved
#in the previous round. The fraction of people who hunted in a group in a round is used as the probability of hunting with every player in that
#group in the next round.Exception is made for those with 0 reputation. I always slacked with them.
#
#
#
#When population is small it should be possible to identify players from their reputation.In this case I do not use groups to determine my action. Rather
# I make a list of who hunted or slacked with me in the last round sorted according to their reputation. I assume that position of a player wont change
#from a round to next round. Of course it wont be true if a player dies but I ignore it for the sake of simplicity.Otherwise it should work because
#in this later stage of game reputation can not change much and because of small population a player most probably retains his/her position.
#I hunt with those who hunted with me in the last round and slack with those who slacked with me in the last round.
#
#
#I did not use the function round_end.


population_small=False
def hunt_choices(round_number, current_food, current_reputation, m,  player_reputations):
    global p,round_num,rep,population_small
    p=len(player_reputations)                       #number of players,declared global for its frequent use  
    round_num=round_number
    rep=player_reputations[:]

    
    if population_small==True:                      #when population is small... 
        hunt_decisions=[] 
        sorted_rep=sorted(player_reputations)[-1::-1] #reputation is sorted from high to low
        for i in player_reputations:
            if i==0:
                hunt_decisions.append('s')          #for alltime slackers
            else:
                if wdwilr[sorted_rep.index(i)]:
                    hunt_decisions.append('h')
                else:
                    hunt_decisions.append('s') 

    elif round_number<=10:
        hunt_decisions=['h' for i in player_reputations]

    elif round_number<=50:
        hunt_decisions=['h' if i!=0 else 's' for i in player_reputations]

    else:
        global behaviour                                            #behaviour is a list containing the information of behaviour of players in the last round
        probability_list=[]                                         #this list will contain the probaility with which I will hunt with members of each group
        for i in behaviour:
            if len(i)!=0:                                           #this probablity is the fraction of hunters in each group in the last round
                probability_list.append(float(i.count(1))/len(i))
            else:
                probability_list.append(probability_list[-1])       #if there is no one in a group in the last round I take the probability of the group 
                                                                    #which is next in reputation as its probability
        for i in player_reputation:
            if i==0:
                hunt_decisions.append('s')
            else:
                hunt_decision.append(decision_maker(probability_list[int(100*i)])) #decision_maker returns 'h' or 's based on given probability
        

    
    
    return hunt_decisions






def hunt_outcomes(food_earnings):
    global p,round_num,rep
    if round_num>p:
        global population_small
        population_small=True
        global wdwilr
        wdwilr=[]                       #wdlir=who did what in last round
        rep1=rep[:]
        for i in range(len(food_earnings)):
            max_rep=0
            max_pos=0
            for j,k in enumerate(rep1):
                if k!=2 and k>max_rep:
                    min_rep=k
                    min_pos=j
            if food_earnings[max_pos]==0 or  food_earnings[max_pos]==1:
                wdwilr.append(1)
            else:
                wdwilr.append(0) 
            rep1[max_pos]=2
        
    elif round_num<50:
        pass

    else:
        global behaviour
        
        behaviour=[[] for i in range(100)]
        for i,j in food_earnings:
            if j==0 or j==1:
                behaviour[rep[i]*100].append(1)
            else:
                behaviour[rep[i]*100].append(0)
            

            


    

def round_end(award, m, number_hunters):
    pass 
    

    



def decision_maker(probability):                #given probabilty this function returns 'h' with that probabilty 
    import random
    if random.random()<probability:
        return 'h'
    else:
        return 's'



